有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何访问ArrayList中对象的特定元素

在我的程序中,我正在读取一个包含电影信息的文件,并提取电影ID和演员姓名。然后,我将每个电影的电影ID和演员名称存储在一个对象中,并将这些对象添加到一个数组列表中。然后我会提示用户输入一个演员的名字,我想检查整个列表,看看演员是否在列表中
我使用while循环来实现这一点:while(!cast.contains(actor1)),但是,我将用户的输入与列表中的对象进行比较,而不是与对象关联的参与者名称

import java.util.Scanner;
import java.io.File;
import java.util.ArrayList;

public class Assignment2 {

    public static void main(String[] args) throws Exception {

        File movieFile = null;
        if(args.length > 0)
            movieFile = new File(args[0]);
        Scanner sc = new Scanner(movieFile);
        String movieLine, actorName;
        int movieID, index1, index2, count = 0; 
        ArrayList<MovieCast> cast = new ArrayList<>();
        MovieCast listObj;
        sc.nextLine(); // skip first line (movie_id, title, cast, crew)
        while(sc.hasNext()) {
            movieLine = sc.nextLine();
            index1 = movieLine.indexOf(',');
            movieID = Integer.parseInt((movieLine.substring(0, index1)).trim());
            index1 = movieLine.indexOf("cast_id"); // in case movie contains name
            if(index1 > -1) {
                index1 = movieLine.indexOf("name", index1);
                index1 += 10; // moved to beginning of actor's name
                index2 = movieLine.indexOf(',', index1);
                actorName = movieLine.substring(index1, index2 - 2);
                listObj = new MovieCast(movieID, actorName);
                cast.add(listObj);
                count++;
            }
        }
        for(int i = 0; i < 10; i++) // for testing
            System.out.println(cast.get(i).movieID + "\t" + cast.get(i).actorName);
        sc.close();

        Scanner scan = new Scanner(System.in);
        String actor1 = " ";
        while(!actor1.isEmpty()) {
            System.out.print("Enter actor 1's name or enter nothing to stop: ");
            actor1 = scan.nextLine();
            if(actor1.length() == 0)
                return;
            while(!cast.contains(actor1)) {
                System.out.println("No such actor.");
                System.out.println("Enter actor 1's name or enter nothing to stop: ");
                actor1 = scan.nextLine();
                if(actor1.length() == 0)
                    return;
            }
        }
    }

}

我想知道如何在对象中访问参与者名称以进行比较


共 (2) 个答案

  1. # 1 楼答案

    按对象属性访问列表的两种方法
    1。迭代

    MovieCast searchMovie(String cast) {
        for(MovieCast movie: movies) {
            if(movie.getCast().equals(cast)) {
                return movie;
            }
        }
        return null;
    }
    

    2。使用地图

    //Key as cast(actor)
    //Value as Movie Object
    HashMap<String, MovieCast> movies= new HashMap<>();
    

    Java8 如果您使用Java8进行编码,那么可以使用流来高效地实现这一点

    public boolean containsCast(final List<MovieCast> list, final String cast){
        return list.stream().filter(o -> o.getCast().equals(cast)).findFirst().isPresent();
    }
    
  2. # 2 楼答案

    使用java8 streams根据您的搜索条件进行筛选/收集

    公共布尔containsCast(最终列表,最终字符串转换){ 返回列表。流()。过滤器(o->;o.getCast()。等于(演员)。findFirst()。isPresent(); }